using System;
using System.Drawing;
using System.Windows.Forms;
using pub.CmdUI.Cmd;
using pub.NET20Fix;

namespace pub.CmdUI.UI.Control
{
    public class ButtonUI : IControlUI
    {
        private Action triggerCallback;
        private ButtonBase button;
        private Bitmap icon;
        private string label;
        private string tooltip;

        public ButtonUI(ButtonBase button)
        {
            this.button = button;
            button.Click += button_Click;
        }

        private void button_Click(object sender, EventArgs e)
        {
            if (triggerCallback != null)
            {
                triggerCallback();
            }
        }

        public Action TriggerCallback
        {
            set { triggerCallback = value; }
        }

        public Bitmap Icon
        {
            set { this.icon = value; }
        }

        public string Label
        {
            set { this.label = value; }
        }

        public string Tooltip
        {
            set { this.tooltip = value; }
        }

        public void Update(ICmd cmd)
        {
            var state = cmd.State;
            if (state == null)
            {
                return;
            }
            button.Visible = state.Visible;
            button.Enabled = state.Enabled;
            var chk = button as CheckBox;
            if (chk != null)
            {
                chk.CheckState = state.Checked.HasValue
                                         ? state.Checked.Value
                                                   ? CheckState.Checked
                                                   : CheckState.Unchecked
                                         : CheckState.Indeterminate;
            }
        }
    }
}